<?php

require "././vendor/autoload.php";

use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Wire\AMQPTable;

//死信
$deadExchangeName = "deadExchange";
$deadQueueName = "deadQueue";
$routeKey = "deadRouteKey";
//普通
$exchangeName = "exchange";
$queueName = "queueName";


$host = PHP_OS == "Linux" ? "127.0.0.1" : "10.0.0.200";
$conn = new AMQPStreamConnection($host, '5672', 'wyqgg', '123456');

$ch = $conn->channel();

$ch->confirm_select();

$ch->set_nack_handler(function (AMQPMessage $message) {
    echo "消息传递失败!" . $message->getBody() . PHP_EOL;
});

$ch->set_ack_handler(function (AMQPMessage $message) {
    echo "消息传递成功!" . $message->getBody() . PHP_EOL;
});


$queueArguments = [
    "x-message-ttl" => 10 * 1000,
    "x-dead-letter-exchange" => $deadExchangeName,
    "x-dead-letter-routing-key" => $routeKey,
];
$arguments = new AMQPTable($queueArguments);
//普通队列
$ch->queue_declare($queueName, false, true, false, false, false, $arguments);
$ch->exchange_declare($exchangeName, AMQPExchangeType::DIRECT, false, true);
$ch->queue_bind($queueName, $exchangeName);

//死信队列
$ch->queue_declare($deadQueueName, false, true);

$ch->exchange_declare($deadExchangeName, AMQPExchangeType::DIRECT, false, true);

$ch->queue_bind($deadQueueName, $deadExchangeName, $routeKey);

$ch->basic_publish(new AMQPMessage('deadMessage'), $exchangeName, '', true);


$ch->close();
$conn->close();